package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.TravelQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;

    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public TravelContent getTravelContent(Long id) {
        return travelContentMapper.selectTravelContent(id);
    }

    public void updateState(Long id, Integer state) {
        travelMapper.updateState(id, state);
    }

    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }


    /**
     * 作者 摘要(模拟AI算法生成) 创建时间 发布时间 最后更新时间
     *
     * @param travel
     * @return
     */
    public AjaxResult saveOrUpdate(Travel travel) {
        AjaxResult result = new AjaxResult();
        TravelContent content = travel.getTravelContent();

        travel.setLastUpdateTime(new Date());//最后更新时间
        travel.setSummary("AI生成中...");//模拟AI摘要生成
        if (travel.getId() == null) {
            travel.setCreateTime(new Date());//创建时间
            travel.setReleaseTime(new Date());//默认就直接审核通过
            travel.setState(Travel.STATE_RELEASE);//审核通过的状态
            travelMapper.insert(travel);//必须先保存文章
            Long id = travel.getId();
            content.setId(id);
            travelContentMapper.insert(content);//再拿到id保存内容
        } else {
            travelMapper.updateByPrimaryKey(travel);
            content.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(content);
        }
        return result.addData(travel.getId());
    }

    public List<Travel> listViewnumTop3(Long destId) {
        return travelMapper.selectViewnumTop3(destId);
    }

    public List<Travel> list() {
        return travelMapper.selectAll();
    }
}
